package cn.tedu.tmall.admin.content.dao.persist.respository.impl;

import cn.tedu.tmall.admin.content.dao.persist.mapper.CategoryMapper;
import cn.tedu.tmall.admin.content.dao.persist.respository.ICategoryRepository;
import cn.tedu.tmall.admin.content.pojo.param.CategoryAddParam;
import cn.tedu.tmall.admin.content.pojo.po.CategoryPO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

@Slf4j
@Repository
public class CategoryRepositoryImpl implements ICategoryRepository {


    /**
     * mybatis的方式
     */
    @Autowired
    //@Qualifier("jdbc")
    //@Qualifier("mybaits")
    //@Qualifier("mybaits-plus")
    private CategoryMapper categoryMapper;

    /**
     * mybatis-puls
     * @param name
     * @return
     */

    /**
     * jdbc
     * @param name
     * @return
     */

    @Override
    public int countByName(String name) {
        //mock数据
        log.debug("通过类别名称查询数据-类别名称:{}",name);
        //Integer count = 0;
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("name",name);
        Integer count = categoryMapper.selectCount(queryWrapper);
        //int count = categoryMapper.countByName(name);
        //Integer count = 1; 我们可以mock数据返回1 表示数据库已经有一条记录
        log.debug("通过类别名称查询数据-类别名称:{},查询结果:{}",name,count);
        return count;
    }

    @Override
    public int save(CategoryPO categoryPO) {
        log.debug("保存类别数据-类别数据:{}",categoryPO);
        //int rows = 0; //保存失败
        //int rows = 1;
        int rows = categoryMapper.insert(categoryPO);
        //int rows = categoryMapper.save(categoryPO);
        log.debug("保存类别数据-类别数据:{},保存结果:{}",categoryPO,rows);
        return rows;
    }

    @Override
    public CategoryPO getCategoryById(Long categoryId) {
        log.debug("通过类别id查询类别-类别id:{}",categoryId);
        CategoryPO categoryPO = categoryMapper.selectById(categoryId);
        log.debug("通过类别id查询类别-类别id:{},类别详情:{}",categoryId,categoryPO);
        return categoryPO;
    }
}
